package com.fancy.mobileschool.ui.adapt;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fancy.mobileschool.R;
import com.fancy.mobileschool.entity.Friend;
import com.fancy.mobileschool.ui.ChatPersonRoomActivity;

import java.util.List;

/**
 * Created by Jackie on 2017/10/15.
 *
 */

public class ChatFriendsAdapt extends RecyclerView.Adapter<FriendItem>{

    private Context context;

    private List<Friend> friends;


    public ChatFriendsAdapt(Context context, List<Friend> friends) {
        this.context = context;
        this.friends = friends;
    }

    @Override
    public FriendItem onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.chat_friends_item, parent, false);

        return new FriendItem(view, context);
    }

    @Override
    public void onBindViewHolder(FriendItem holder, int position) {
        holder.getMotto().setText(friends.get(position).motto);
        holder.getNickName().setText(friends.get(position).name);
        holder.setFriendID(friends.get(position).id);
    }

    @Override
    public int getItemCount() {
        return friends.size();
    }

}


class FriendItem extends RecyclerView.ViewHolder implements View.OnClickListener {

    private ImageView icon;

    private TextView nickName;

    private TextView motto;

    private Context context;

    private String friendID;

    public FriendItem(View itemView, Context context) {
        super(itemView);
        this.context = context;
        icon = (ImageView) itemView.findViewById(R.id.chat_friends_item_icon);
        nickName = (TextView) itemView.findViewById(R.id.chat_friends_item_nickname);
        motto = (TextView) itemView.findViewById(R.id.chat_friends_item_motto);
        setOnclick(itemView);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(context, ChatPersonRoomActivity.class);
        intent.putExtra("friendID", friendID);
        intent.putExtra("friendName", nickName.getText());
        context.startActivity(intent);
    }

    public ImageView getIcon() {
        return icon;
    }

    public void setIcon(ImageView icon) {
        this.icon = icon;
    }

    public TextView getNickName() {
        return nickName;
    }

    public void setNickName(TextView nickName) {
        this.nickName = nickName;
    }

    public TextView getMotto() {
        return motto;
    }

    public void setMotto(TextView motto) {
        this.motto = motto;
    }

    public void setFriendID(String friendID) {
        this.friendID = friendID;
    }

    private void setOnclick(View view){
        view.setOnClickListener(this);
    }


}